﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using ProjetPacman;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace pacman
{
    abstract class Personnage
    {
        public const int NORMAL=1;
        public const int RAPIDE=2;

        public const int GAUCHE = 0;
        public const int DROITE = 1;
        public const int HAUT = 2;
        public const int BAS = 3;

        private Vector2 _position;

        private int _direction;

        public int Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }

        private int nextDirection;

        public int NextDirection
        {
            get { return nextDirection; }
            set { nextDirection = value; }
        }

        protected int _vitesse;
        public bool _vulnerable;
        protected ListAnimation animations;


        public float convertReal(float position)
        {
            position *= Frame.TILE_X;
            return position;
        }

        public float convertUnreal(float position)
        {
            position = (position - position % Frame.TILE_Y) / Frame.TILE_Y;
            return position;
        }

        public Vector2 convertReal(Vector2 position)
        {
            position.X = convertReal(position.X);
            position.Y = convertReal(position.Y);
            return position;
        }

        public Vector2 convertUnreal(Vector2 position)
        {
            position.X = convertUnreal(position.X);
            position.Y = convertUnreal(position.Y);
            return position;
        }

        public Vector2 Position
        {
            get { return convertUnreal(_position); }
            set { _position = convertReal(value); }
        }

        public float PositionX
        {
            get { return convertUnreal(_position).X; }
            set { _position.X = convertReal(value); }
        }

        public float PositionY
        {
            get 
            { 
                Vector2 position = _position;
                position.Y /= Frame.TILE_Y;
                return position.Y;
            }
            set
            {
                float position = value;
                position*= Frame.TILE_Y;
                _position.Y = position;
            }
        }

        public float PositionRealX
        {
            get { return _position.X; }
            set { _position.X = value; }
        }

        public float PositionRealY
        {
            get { return _position.Y; } 
            set { _position.Y = value; }
        }

        public Vector2 PositionReal
        {
            get { return _position; }
            set { _position = value; }
        }

        public void ChangeEtat()
        {
            _vulnerable=!_vulnerable;
        }

        public Vector2 Raccourci(Vector2 position)
        {
            if (position.X < 0) position.X = Levels.VY - 1;
            else if (position.Y < 0) position.Y = Levels.VX - 1;
            else if (position.X >= Levels.VY) position.X = 0;
            else if (position.Y >= Levels.VX) position.Y = 0;

            return position;
        }

        public void Next(int direction)
        {
            switch (direction)
            {
                case DROITE:
                    PositionRealX += _vitesse * 2f;
                    break;
                case GAUCHE:
                    PositionRealX -= _vitesse * 2f;
                    break;
                case HAUT:
                    PositionRealY -= _vitesse * 2f;
                    break;
                case BAS:
                    PositionRealY += _vitesse * 2f;
                    break;
                default:
                    return;
            }
            if (Position != Raccourci(Position)) Position = Raccourci(Position);
        }

        public Vector2 NextBlock(int direction)
        {
            switch (direction)
            {
                case DROITE:
                    return Raccourci(new Vector2(PositionX + 1f, PositionY));
                case GAUCHE:
                    return Raccourci(new Vector2(PositionX - 1f, PositionY));
                case HAUT:
                    return Raccourci(new Vector2(PositionX, PositionY - 2f));
                case BAS:
                    return Raccourci(new Vector2(PositionX, PositionY + 2f));
                default:
                    return Position;
            }
        }

        public bool finAnimation()
        {
            return (_position.X % Frame.TILE_X == 0 && _position.Y % Frame.TILE_Y == 0);
        }

        public Personnage(int niveau)
        {
            animations = new ListAnimation();
        }

        public void Draw(SpriteBatch spriteBatch, long temps)
        {
            animations.Refresh(temps, _direction);
            animations.Draw(spriteBatch, _position, _direction);
        }
    }
}
